//
//  AppDelegate.swift
//  SwiftDemo
//
//  Created by hfzhangzhang on 2018/3/16.
//  Copyright © 2018年 hfzhangzhang. All rights reserved.
//

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool{
        self.setUIGlobalAppearance();
        self.setUIPartAppearance();
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
    }

    func applicationDidEnterBackground(_ application: UIApplication) {

    }

    func applicationWillEnterForeground(_ application: UIApplication) {

    }

    func applicationDidBecomeActive(_ application: UIApplication) {

    }

    func applicationWillTerminate(_ application: UIApplication) {
        self.saveContext()
    }
    

    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "Model")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()
    

    func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }
    
}

extension AppDelegate {
    //全局修改外观
    func setUIGlobalAppearance() {
        UINavigationBar.appearance().tintColor = UIColor.white; 
        UINavigationBar.appearance().barTintColor = UIColor.red;
        let dict:NSDictionary = [kCTForegroundColorAttributeName: UIColor.white,kCTFontAttributeName : UIFont.boldSystemFont(ofSize: 18)]
        UINavigationBar.appearance().titleTextAttributes = dict as? [NSAttributedStringKey : Any];
        UINavigationBar.appearance().isTranslucent = false;
        
    }
    //部分修改外观
    func setUIPartAppearance() {
      
        UITabBar.appearance(whenContainedInInstancesOf: [MainTabBarViewController.self]).tintColor = UIColor.green;
        UITabBar.appearance(whenContainedInInstancesOf: [MainTabBarViewController.self]).barTintColor = UIColor.blue;
        
    }

}

extension AppDelegate{
   
}


